package com.shaw.study.test.dao;

import com.shaw.study.springx.transaction.TransactionManager;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by asus on 2018/5/20.
 */
public class UserDao implements IUserDao {
    @Override
    public String getUserName(String Id) {
        String username = "";
        try {
            Connection connection = TransactionManager.getInstances().getConnection();
            String sql = "select username from user where id = 1";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                username = rs.getString("username");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return username;
    }

    @Override
    public boolean addUser(String id, String userName) {
        boolean rs = false;
        try {
            Connection connection = TransactionManager.getInstances().getConnection();
            String sql = "insert into user (username,id) value ('"+userName+"','"+id+"')";
            Statement statement = connection.createStatement();
            rs = statement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
